$(function(){

		var strCookie = document.cookie;
            var arrCookie = strCookie.split("; ");
            for(var i = 0; i < arrCookie.length; i++){
                var arr = arrCookie[i].split("=");
             	var id = arr[0];
             	$.cookie('checknum','',{expires:-1});
             	var regPhone = /^1([3578]\d|4[57])\d{8}$/;
             	if( regPhone.test(id) != true){
             		writeInto(id);
             	}
                	//这里的判断是用来获取特定的，我不需要
  				 // if('693417990539' == arr[0]){
       //          	console.log(arr[0]);
       //              return arr[1];
       //          }
            }

	function writeInto(id){
			var str = $.cookie(id);
			var testJson = eval("(" + str + ")");
			console.log(testJson);
			var brand = testJson[0].Brand;
			var title = testJson[0].Title;
			var price = testJson[0].Price;
			var num;
			if(testJson[1] == undefined){
				num =0;
			}else{
				num = testJson[1].Num;
			}
			var totalmoney = num * price;
			var img = testJson[0].Img;
			var color = testJson[0].Color;
			var memory = testJson[0].Memory;
			var str = ''+color+','+memory+'';
			createShow(id,brand,title,price,num,totalmoney,img,str);	
		}
	function createShow(id,brand,title,price,num,totalmoney,img,str){
		$('	<tr><td class="border-v cart-check" width="40"><input type="checkbox" name="" checked="checked"></td><td class="border-v"><a href="#">'+ img +'</a></td><td class="border-v" alt="title"><a href="#">'+ title +'</a></td><td class="border-v">'+ str +'</td><td class="border-v" alt="price">'+price+'</td><td class="border-v"><button class="num-button-re">-</button><input class="goods-option" style="width:40px;height:20px;text-align:center;float: left;" value="'+num+'" alt="Num"><button class="num-button-add">+</button></td><td class="border-v" alt="id">'+id+'</td><td class="border-v" alt="totalmoney">'+totalmoney+'.00</td><td class="border-v option"><a href="javascript:void(0);" class="delete-content">删除</a><a href="#">移入收藏夹</a><a href="#"><span>定制包装</span></a></td></tr>').appendTo($('.cart-goods tbody'))
	}
	
	$('.num-button-re').each(function(index){
		$(this).click(function() {
			var i = Number($('[alt="Num"]').eq(index).val());
			var n = $('.red-number').text();
			var p = Number($('[alt="price"]').eq(index).text());
			var t = 0;
		if( $(this).parents('tr').find(':input').prop('checked') == true){
			if(i>1){
				i--;
				n--;
				t = i * p;
				$('[alt="Num"]').eq(index).val(i);
				$('.red-number').text(n);
				$('[alt="totalmoney"]').eq(index).text(t);
			}
			var total = 0;
				$('[alt="totalmoney"]').each(function(index, el) {
				total += Number($('[alt="totalmoney"]').eq(index).text());
				$('.textm strong').text(total);
				});
			}
		});
	})


	$('.num-button-add').each(function(index){
		$(this).click(function() {
			var i = $('[alt="Num"]').eq(index).val();
			var n = $('.red-number').text();
			var p = Number($('[alt="price"]').eq(index).text());
			var t =0 ;
		if( $(this).parents('tr').find(':input').prop('checked') == true){
			if(i>=1){
				i++;
				n++;
				t = i * p;
				$('[alt="Num"]').eq(index).val(i);
				$('.red-number').text(n);
				$('[alt="totalmoney"]').eq(index).text(t);
			}
			var total = 0;
				$('[alt="totalmoney"]').each(function(index, el) {
				total += Number($('[alt="totalmoney"]').eq(index).text());
				$('.textm strong').text(total);
				});
			}
		});
	})

	$('.delete-content').each(function(index){
		$(this).click(function(){
			var del = $(this).parents('tr').find($('td[alt="id"]')).text();
			$.cookie(del, '', { expires: -1 });
			var repote = Number($('.textm strong').text()) - Number($(this).parents('tr').find($('[alt="totalmoney"]')).text());
			var repoteNum = Number($('.red-number').text()) - Number($(this).parents('tr').find($('[alt="Num"]')).val());
			$(this).parents('tr').remove();
			
			if (repote < 0){
					repote = 0;
			}
			if (repoteNum < 0){
					repoteNum = 0;
			}  
			console.log(Number($(this).parents('tr').find($('[alt="totalmoney"]')).text()));	
				$('.textm strong').text(repote);
				$('.red-number').text(repoteNum);
		})
	})

	//商品总量输出
	var totalNum = 0;
	$('[alt="Num"]').each(function(index, el) {
		totalNum += Number($('[alt="Num"]').eq(index).val());
		$('.red-number').text(totalNum);
	});
	//总价格输出
	var total = 0;
	$('[alt="totalmoney"]').each(function(index, el) {
		total += Number($('[alt="totalmoney"]').eq(index).text());
		$('.textm strong').text(total);
	});



	$(':checkbox').each(function(index){
		$(this).click(function(event) {
			var money = Number($('.textm strong').text());
			var tnum = 	Number($('.red-number').text());
			var n = $(this).parents('tr').find($('[alt="totalmoney"]')).text();
			var m = $(this).parents('tr').find($('[alt="Num"]')).val();
			if ($(this).prop('checked') == true){
				money += Number($(this).parents('tr').find($('[alt="totalmoney"]')).text());
				tnum += Number($(this).parents('tr').find($('[alt="Num"]')).val());
			}else{
				money -= n;
				tnum -=m;
			}
			$('.textm strong').text(money);
			$('.red-number').text(tnum);
		})
	})


	$('.submit-price').click(function(){
		alert('结算结束：你已破产');
	})

})